package com.test.platform.module.perf.controller;

import com.test.common.response.ResponseResult;
import com.test.common.utils.FileUploadUtils;
import com.test.platform.module.perf.service.PerfResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Api(value = "value", tags = "性能平台资源文件管理")
@RestController
@RequestMapping("/perf/resource")
@ResponseResult
public class PerfResourceController {

    @Resource
    private PerfResourceService perfResourceService;

    @ApiOperation("上传资源文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "__file", name = "file", value = "待上传的文件", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "uploadId", value = "用于记录进度的ID", required = true),
    })

    @PostMapping("/upload")
    public Object fileUpload(HttpServletRequest request, @RequestParam("uploadId") String uploadId) {
        return perfResourceService.fileUpload(FileUploadUtils.fileUpload(request, uploadId));
    }

    @ApiOperation("查询文件上传进度")
    @GetMapping("/upload/process")
    public Object uploadProcess(HttpServletRequest request, @RequestParam("uploadId") String uploadId) {
        return FileUploadUtils.uploadProcess(request, uploadId);
    }

    @ApiOperation("清理文件上传进度记录")
    @GetMapping("/clear/process")
    public Object clearProcess(HttpServletRequest request, @RequestParam("uploadId") String uploadId) {
        return FileUploadUtils.clearProcess(request, uploadId);
    }

    @ApiOperation("下载文件")
    @GetMapping("/download")
    public void download(@RequestParam("id") int id, HttpServletResponse response) {
        perfResourceService.download(id, response);
    }

    @ApiOperation("删除文件")
    @GetMapping("/delete")
    public Object delete(@RequestParam("id") int id) {
        return perfResourceService.delete(id);
    }

    @ApiOperation("查找所有脚本文件")
    @GetMapping("/find/all")
    public Object findAll(@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize) {
        return perfResourceService.findAll(pageNo, pageSize);
    }

    @ApiOperation("查找所有脚本文件")
    @GetMapping("/getAllNames")
    public Object getAllNames() {
        return perfResourceService.getAllNames();
    }

    @GetMapping("/getDetail")
    public Object getDetail(int id) {
        return perfResourceService.getDetail(id);
    }
}
